package com.usian.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {


    @Override
    public ResponseResult findNameByPage(ChannelDto channelDto) {
        if (channelDto == null) {
            return ResponseResult.errorResult (AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        channelDto.checkParam ();
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<> ();
        lambdaQueryWrapper.like (StringUtils.isNotEmpty (channelDto.getName ()), AdChannel::getName, channelDto.getName ());
        Page<AdChannel> page = new Page<> (channelDto.getPage (), channelDto.getSize ());
        IPage<AdChannel> iPage = this.page (page, lambdaQueryWrapper);
        PageResponseResult responseResult = new PageResponseResult (channelDto.getPage (), channelDto.getSize (), Integer.valueOf (iPage.getTotal () + ""));
        responseResult.setData (iPage.getRecords ());
        return responseResult;
    }


}
